<table class="table qc-table table-striped table-responsive summary">
   <thead>
     <tr><th colspan="2" class="summary"><i class="fa fa-thumb-tack"></i> IBrokerageFactory</th></tr>
   </thead>
   <tbody>
      <tr><td width="30%">Primary Role</td><td>Create and initialize a brokerage instance.</td></tr>
      <tr><td>Interface</td><td><a href="https://github.com/QuantConnect/Lean/blob/master/Common/Interfaces/IBrokerageFactory.cs">IBrokerageFactory.cs</a></td></tr>
      <tr><td>Example</td><td><a href="https://github.com/QuantConnect/Lean/blob/master/Brokerages/GDAX/GDAXBrokerageFactory.cs">GDAXBrokerageFactory.cs</a></td></tr>
      <tr><td>Target Location</td><td><a href="https://github.com/QuantConnect/Lean/blob/master/Brokerages/GDAX/">In Brokerages Folder in Brokerages Solution</a></td></tr>
   </tbody>
</table>

<p>
The <a href="https://github.com/QuantConnect/Lean/blob/master/Common/Interfaces/IBrokerageFactory.cs">IBrokerageFactory</a> creates brokerage instances with a <a href="https://github.com/QuantConnect/Lean/blob/master/Common/Packets/LiveNodePacket.cs#L37">Job Packet</a> which configures LEAN. It contains the name of the selected brokerage to create, which is used to create the right BrokerageFactory type. The configuration <code>live-mode-brokerage</code> key is used to set the brokerage name.
</p>

<h4>1.1 Create Authentication Configuration</h4>
<p>
In the <a href="https://github.com/QuantConnect/Lean/blob/master/Launcher/config.json">configuration file</a> add a few key-values with your brokerage configuration information. This will be used for most local debugging and testing as the default. E.g. <code>oanda-access-token</code> and <code>oanda-account-id</code>. These will be copied to the job packet which contains a matching field <a href="https://github.com/QuantConnect/Lean/blob/master/Common/Packets/LiveNodePacket.cs#L43">BrokerageData</a>. This is a dictionary of &lt;string,string&gt; pairs.
</p>

<h4>1.2 Create Brokerage Data</h4>
<p>
By default the <code>IBrokerageFactory.BrokerageData</code> implementation should load all required configuration from the config file using the <code>Config</code> class. E.g. <code>Config.Get("oanda-access-token")</code>. This can be a simple <a href="https://github.com/QuantConnect/Lean/blob/master/Brokerages/GDAX/GDAXBrokerageFactory.cs#L47">pass through</a> to the config for most brokerages.
</p>

<h4>1.3 Create Brokerage Model</h4>
<p>
Brokerage Models tell LEAN what order types a brokerage supports, whether we're allowed to update an order, and what transaction models to use for fills. It is important to do but something we can come back to later. For now, we should just create a stub implementation which we'll extend and improve later. This file <code>MyBrokerageBrokerageModel.cs</code> lives in the <code>/Common/Brokerages</code> folder. For now, you can make it an empty implementation inheriting from the <code>DefaultBrokerageModel</code>. See this example of a partially implemented model <a href="https://github.com/QuantConnect/Lean/blob/master/Common/Brokerages/GDAXBrokerageModel.cs">here</a>. Set your empty placeholder model to the BrokerageModel <a href="https://github.com/QuantConnect/Lean/blob/master/Brokerages/GDAX/GDAXBrokerageFactory.cs#L58">property</a>.
</p>

<h4>1.4 Create Brokerage Instance in Factory</h4>
<p>
The Brokerage Factory uses a job packet to create an initialized brokerage instance. This happens in the <code>CreateBrokerage()</code> method. You should assume the <code>job</code> has the best source of data <i>not</i> the class BrokerageData property. The <code>BrokerageData</code> property on the factory are the starting default values sourced from config which can be overridden by a runtime job.
</p>

<p>
Given our <a href="https://github.com/QuantConnect/Lean/blob/master/Common/Interfaces/IBrokerage.cs">IBrokerage</a> implementation hasn't been started yet let's make a placeholder file for our brokerage with the methods stubbed out: <code>MyBrokerage.cs</code> in the <code>MyBrokerage</code> folder, in the <code>Brokerages</code> solution. All the methods should throw a <code>new NotImplementedException()</code> for now except for the constructor which should save any required authentication data to private variables. The <code>CreateBrokerage()</code> method should create a brokerage object but not <i>connect</i> to the brokerage. The connection is done later in the LEAN start-up process.
</p>

<h4>1.5 Create Configuration Environment</h4>
<p>
In the config file LEAN has helper <em>environments</em> which group configuration flags together and override the root configuration values. You should make a <code>mybrokerage-live</code> environment for your brokerage which specifies the brokerage type name for <code>live-mode-brokerage</code>. You should copy the <a href="https://github.com/QuantConnect/Lean/blob/master/Launcher/config.json#L132">paper-trading brokerage</a> setup to start. You should set the <code>environment</code> <a href="https://github.com/QuantConnect/Lean/blob/master/Launcher/config.json#L9">value</a> to your new brokerage environment for testing.
</p>

<div class="tip">
  <i class="fa fa-lightbulb-o"></i><span class="tip-title">Tip:</span>
  <p>In the <code>IBrokerageFactory</code> examples, you'll see code like this: <code>Composer.Instance.AddPart&lt;IDataQueueHandler&gt;(dataQueueHandler)</code>, which is adding parts to the "Composer". The Composer is a system in LEAN for loading types dynamically. In this case, it is adding an instance of the DataQueueHandler for the brokerage to the composer. You can think of the composer as a library, and adding parts is like adding books to its collection. But we'll come back to this later...</p>
</div>

<table class="table qc-table table-striped table-responsive">
   <thead>
     <tr><th colspan="2" class="summary"><i class="fa fa-check"></i> Stage 1: Checklist</th></tr>
   </thead>
   <tr><td width="10px"><input type="checkbox"/></td><td>Configuration keys and placeholder values for brokerage authentication requirements.</td></tr>
   <tr><td width="10px"><input type="checkbox"/></td><td>Created folder for brokerage in Brokerages solution; with <code>MyBrokerageFactory.cs</code>.</td></tr>
   <tr><td width="10px"><input type="checkbox"/></td><td>Implemented all interfaces of the BrokerageFactory (some with stub implementations).</td></tr>
   <tr><td width="10px"><input type="checkbox"/></td><td>Create a stub MyBrokerage.cs with Not Implemented exceptions.</td></tr>
   <tr><td width="10px"><input type="checkbox"/></td><td>Create a stub MyBrokerageBrokerageModel.cs inheriting from DefaultBrokerageModel.</td></tr>
   <tr><td width="10px"><input type="checkbox"/></td><td>Created a <code>mybrokerage</code> live configuration environment specifying your class.</td></tr>
   <tr><td width="10px"><input type="checkbox"/></td><td>Set the <code>environment</code> configuration to your new brokerage environment.</td></tr>
</table>

<div class="tip">
  <i class="fa fa-bolt"></i><span class="tip-title">Action:</span>
  <p> Build the solution. Although <em>running</em> won't work the stub implementations should still build.</p>
</div>